//
//  RequestManager.swift
//  MySwiftProject
//
//  Created by 蓓蕾 on 2021/11/5.
//

import UIKit
import Alamofire

let BaseUrl = "https://www.titaids.com"

private let RequestManagerShareInstance = RequestManager()

class RequestManager{
    class var shareInstance:RequestManager {
        return RequestManagerShareInstance
    }
}

extension RequestManager{
//    _ error:Error)->()
    
    func getRequest(UrlString:String,parama:[String:Any]?,success:@escaping(DataResponse<Any>)->Void,failure:@escaping(DataResponse<Any>)->Void){
        let pathUrl = BaseUrl+UrlString
        
        Alamofire.request(pathUrl,method:.get,parameters:parama).responseJSON{(response) in
            if(response.result.isSuccess)
            {
//                print("\(response)")
                success(response)
            }
            else
            {
                print("请求失败")
//                failure(response.result.value?.fastestEncoding)
                    
                
            }
        }
    }
    
    
    
    func postRequest(UrlString:String,parama:[String:Any]?,success:@escaping(DataResponse<Any>)->Void,failure:@escaping(DataResponse<Any>)->Void){
        let pathUrl = BaseUrl+UrlString
        
        Alamofire.request(pathUrl,method:.post,parameters:parama).responseJSON{(response) in
            if(response.result.isSuccess)
            {
//                print("\(response)")
                success(response)
            }
            else
            {
                print("请求失败")
                failure(response)
            }
        }
    }
    
//    func getRequest(UrlString:String,parama:[String:Any]?,success:@escaping(_ response:[String:AnyObject])->(),failure:@escaping(_ error:Error)->()){
//        let pathUrl = BaseUrl+UrlString
//        Alamofire.request(pathUrl,method:.get,parameters:parama).responseJSON{(response) in
//            if(response.result.isSuccess)
//            {
//                print("\(response)")
//            }
//            else
//            {
//                print("请求失败")
//            }
//        }
//    }
}

//extension Result: CustomStringConvertible {
//    /// The textual representation used when written to an output stream, which includes whether the result was a
//    /// success or failure.
//    public var description: String {
//        switch self {
//        case .success:
//            return "SUCCESS"
//        case .failure:
//            return "FAILURE"
//        }
//    }
//}
//
//// MARK: - CustomDebugStringConvertible
//
//extension Result: CustomDebugStringConvertible {
//    /// The debug textual representation used when written to an output stream, which includes whether the result was a
//    /// success or failure in addition to the value or error.
//    public var debugDescription: String {
//        switch self {
//        case .success(let value):
//            return "SUCCESS: \(value)"
//        case .failure(let error):
//            return "FAILURE: \(error)"
//        }
//    }
//}


//class RequestManager: NSObject {
//
//}
